package com.medical.admin.patient.service.Impl;

import com.medical.admin.patient.common.dto.MedicalRecordDto;
import com.medical.admin.patient.entity.MedicalRecord;
import com.medical.admin.patient.mapper.MedicalRecordMapper;
import com.medical.admin.patient.service.MedicalRecordService;
import com.qf.medical.ex.ServiceException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

@Service
public class MedicalRecordServiceImpl implements MedicalRecordService {
    @Resource
    private MedicalRecordMapper medicalRecordMapper;

    @Override
    public MedicalRecordDto searchMedicalRecord(Long patientId) {
        MedicalRecord medicalRecord = medicalRecordMapper.selectByPatientId(patientId);
        if (ObjectUtils.isEmpty(medicalRecord)){
            throw new ServiceException(ResponseCode.MEDICAL_RECORD_SELECT_ERROR);
        }
        return BeanConvertUtils.convertTo(medicalRecord,MedicalRecordDto::new);
    }

    @Override
    public Integer addMedicalRecord(MedicalRecordDto medicalRecordDto) {
        int count = medicalRecordMapper.insert(BeanConvertUtils.convertTo(medicalRecordDto, MedicalRecord::new));
        if (count <= 0){
            throw new ServiceException(ResponseCode.MEDICAL_RECORD_ADD_ERROR);
        }
        return count;
    }

    @Override
    public Integer modifyMedicalRecord(MedicalRecordDto medicalRecordDto) {
        int count = medicalRecordMapper.updateById(BeanConvertUtils.convertTo(medicalRecordDto, MedicalRecord::new));
        if (count <= 0){
            throw new ServiceException(ResponseCode.MEDICAL_RECORD_UPDATE_ERROR);
        }
        return count;
    }

    @Override
    public Integer removeMedicalRecord(Long patientId) {
        int count = medicalRecordMapper.deleteByPatientId(patientId);
        if (count <= 0){
            throw new ServiceException(ResponseCode.MEDICAL_RECORD_DELETE_ERROR);
        }
        return count;
    }
}
